﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using MySql.Data;
using MySql.Data.MySqlClient;
using CodeFactory.IDAL;

namespace CodeFactory.MySql
{
    public class MySqlHelper : IDbHelper
    {
        private MySqlConnection connection;
        private string connectionString;
        private string connectionType;
        private int queryNum;

        public IDbConnection Connection
        {
            get { return this.connection; }
        }

        public string ConnectionString
        {
            get { return this.connectionString; }
            set { this.connectionString = value; }
        }

        public string ConnectionType
        {
            get { return this.connectionType; }
        }

        public int QueryNum
        {
            get { return this.queryNum; }
        }

        public ConnectionState State
        {
            get { return this.connection.State; }
        }

        public MySqlHelper()
        {
            this.connection = new MySqlConnection();
            this.connectionString = string.Empty;
            this.connectionType = "MySql";
            this.queryNum = 0;
        }

        public MySqlHelper(string connString)
        {
            this.connectionString = connString;
            this.connectionType = "MySql";
            this.queryNum = 0;
            this.connection = new MySqlConnection(this.connectionString);
        }

        public void Open()
        {
            if (this.connection.State != ConnectionState.Open)
                this.connection.Open();
        }

        public void Close()
        {
            if (this.connection.State == ConnectionState.Open)
            {
                this.connection.Close();
            }
        }

        public void Dispose()
        {
            this.Close();
        }

        public int ExecuteNonQuery(string sqlString)
        {
            MySqlCommand command = new MySqlCommand();
            command.Connection = connection;
            command.CommandText = sqlString;
            int result = command.ExecuteNonQuery();
            this.queryNum++;
            return result;
        }

        public int ExecuteNonQuery(string sqlString, IDbDataParameter[] sqlParas)
        {
            MySqlCommand command = new MySqlCommand();
            command.Connection = connection;
            command.CommandText = sqlString;
            foreach (IDbDataParameter p in sqlParas)
            {
                command.Parameters.Add(p);
            }
            int result = command.ExecuteNonQuery();
            this.queryNum++;
            return result;
        }

        public object ExecuteScalar(string sqlString)
        {
            MySqlCommand command = new MySqlCommand();
            command.Connection = connection;
            command.CommandText = sqlString;
            object result = command.ExecuteScalar();
            this.queryNum++;
            return result;
        }

        public object ExecuteScalar(string sqlString, IDbDataParameter[] sqlParas)
        {
            MySqlCommand command = new MySqlCommand();
            command.Connection = connection;
            command.CommandText = sqlString;
            foreach (IDbDataParameter p in sqlParas)
            {
                command.Parameters.Add(p);
            }
            object result = command.ExecuteScalar();
            this.queryNum++;
            return result;
        }

        public IDataReader ExecuteDataReader(string sqlString)
        {
            MySqlCommand command = new MySqlCommand();
            command.Connection = connection;
            command.CommandText = sqlString;
            IDataReader result = command.ExecuteReader();
            this.queryNum++;
            return result;
        }

        public IDataReader ExecuteDataReader(string sqlString, IDbDataParameter[] sqlParas)
        {
            MySqlCommand command = new MySqlCommand();
            command.Connection = connection;
            command.CommandText = sqlString;
            foreach (IDbDataParameter p in sqlParas)
            {
                command.Parameters.Add(p);
            }
            IDataReader result = command.ExecuteReader();
            this.queryNum++;
            return result;
        }

        public DataTable ExecuteDataTable(string sqlString)
        {
            MySqlCommand command = new MySqlCommand();
            command.Connection = connection;
            command.CommandText = sqlString;
            DataTable result = new DataTable();
            MySqlDataAdapter da = new MySqlDataAdapter(command);
            da.Fill(result);
            this.queryNum++;
            return result;
        }

        public DataTable ExecuteDataTable(string sqlString, IDbDataParameter[] sqlParas)
        {
            MySqlCommand command = new MySqlCommand();
            command.Connection = connection;
            command.CommandText = sqlString;
            foreach (IDbDataParameter p in sqlParas)
            {
                command.Parameters.Add(p);
            }
            DataTable result = new DataTable();
            MySqlDataAdapter da = new MySqlDataAdapter(command);
            da.Fill(result);
            this.queryNum++;
            return result;
        }

        public DataTable ExecuteSchemaTable(Guid schema, object[] objs)
        {
            return null;
        }
    }
}
